package service;

import mapper.SegmentStrategyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.*;

import java.util.ArrayList;
import java.util.List;

@Service
public class SegmentStrategyServiceImpl implements SegmentStrategyService {
    @Autowired
    private SegmentStrategyMapper segmentStrategyMapper;

    @Override
    public List<SegmentStrategy> findAllSegmentStrategies() throws Exception {
        return segmentStrategyMapper.findAllSegmentStrategies();
    }

    @Override
    public List<SegmentStrategyCustom> findAllSegmentStrategyCustom() throws Exception {
        return segmentStrategyMapper.findAllSegmentStrategyCustom();
    }

    @Override
    public Result addSegmentStrategy(SegmentStrategy segmentStrategy) {
        Result result = new Result();
        try {
            segmentStrategyMapper.addSegmentStrategy(segmentStrategy);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("添加失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("添加成功！");
        return result;
    }

    @Override
    public Result deleteSegmentStrategyById(int id) {
        Result result = new Result();
        try {
            segmentStrategyMapper.deleteSegmentStrategyById(id);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("删除失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("删除成功！");
        return result;
    }

    @Override
    public Result updateSegmentStrategyById(int id, SegmentStrategy segmentStrategy) {
        Result result = new Result();
        try {
            segmentStrategy.setId(id);
            segmentStrategyMapper.updateSegmentStrategyById(segmentStrategy);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("修改失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("修改成功！");
        return result;
    }

    @Override
    public Result addSegment(Segment segment) {
        Result result = new Result();
        try {
            segmentStrategyMapper.addSegment(segment);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("添加失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("添加成功！");
        return result;
    }

    @Override
    public Result deleteSegment(Segment segment) {
        Result result = new Result();
        try {
            segmentStrategyMapper.deleteSegment(segment);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("删除失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("删除成功！");
        return result;
    }

    @Override
    public List<ComboboxItem> findSegmentStrategyForComb() throws Exception {
        List<SegmentStrategy> segmentStrategyListList = segmentStrategyMapper.findAllSegmentStrategies();
        List<ComboboxItem> list = new ArrayList<>();
        for (SegmentStrategy segmentStrategy : segmentStrategyListList) {
            ComboboxItem item = new ComboboxItem();
            item.setId(segmentStrategy.getId());
            item.setText(segmentStrategy.toString());
            list.add(item);
        }
        return list;
    }
}
